package com.cal2sync;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.cal2sync.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class create_rule extends Activity {
    /** Called when the activity is first created. */
	LinearLayout l;
	//EditText event_type;
	Spinner spinner, event_spinner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_rule);
        
        spinner = (Spinner) findViewById(R.id.action_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.action_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        event_spinner = (Spinner) findViewById(R.id.event_spinner);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
                this, R.array.event_array, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        event_spinner.setAdapter(adapter1);
        
        //event_type=(EditText) findViewById(R.id.event_type_edittext);
        l=(LinearLayout) findViewById(R.id.main);
		final Animation inslide = AnimationUtils.loadAnimation(this, R.anim.lefttoright);
		final Animation slide = AnimationUtils.loadAnimation(this, R.anim.righttoleft);

		l.startAnimation(inslide);
		inslide.setFillAfter(true);
		inslide.setAnimationListener(new AnimationListener(){
			public void onAnimationEnd(Animation animation){
        
        final Button button1 = (Button) findViewById(R.id.create_rule_btn);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	l.startAnimation(slide);
				slide.setFillAfter(true);
				slide.setAnimationListener(new AnimationListener(){
					public void onAnimationEnd(Animation animation){
						HttpClient httpclient = new DefaultHttpClient();
						//HttpPost httppost = new HttpPost("http://10.0.2.2/insert_rules.php");
						HttpPost httppost = new HttpPost("http://192.168.16.1/insert_rules.php");

						try {
							// Add your data
							List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
							nameValuePairs.add(new BasicNameValuePair("username", GlobalData.username));
							nameValuePairs.add(new BasicNameValuePair("event_type", event_spinner.getSelectedItem().toString()));
							nameValuePairs.add(new BasicNameValuePair("action", spinner.getSelectedItem().toString()));
							httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

							// Execute HTTP Post Request
							HttpResponse response = httpclient.execute(httppost);
							
							System.out.println("Response ->"+response.toString());
							//Intent i = new Intent(getBaseContext(),last_screen.class);
							Intent i = new Intent(getBaseContext(),display_rules.class);
        					startActivity(i);
							
						} catch (ClientProtocolException e) {
							System.out.println("exception 1");
							// TODO Auto-generated catch block
						} catch (IOException e) {
							e.printStackTrace();
							System.out.println("error ");
							// TODO Auto-generated catch block
						}
            	 
					}

					@Override
					public void onAnimationRepeat(Animation arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub

					}
				});
            }
        });
        
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}
		});
    }
}